<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['get_cihui','get_children_category'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

	public function get_cihui(Request $request){
		$keyword=$request->param('keyword/s','');
		$rs=Db::name('cihui')->where(['word'=>['like',"%$keyword%"]])->order('weigh desc')->limit(20)->select();
		$this->success('请求成功',$rs);
	}

	public function submit_dingyue(Request $request){

		$rs=Db::name('qikandingyue')->insertGetId([
			'user_id'=>$this->auth->id,
			'order_sn'=>getMillisecond(),
			'createtime'=>time(),
			'dingyueshichang'=>$request->param('dingyueshichang/s',''),
			'dinggou_date'=>$request->param('dinggou_date/s',''),
			'price'=>$request->param('price/f',0),
			'dinggoufenshu'=>$request->param('dinggoufenshu/d',1),
			'money'=>$request->param('money/f',0),
			'shouhuoren'=>$request->param('shouhuoren/s',''),
			'tel'=>$request->param('tel/s',''),
			'address'=>$request->param('address/s',''),
			'beizhu'=>$request->param('beizhu/s',''),
			'qikan_id'=>$request->param('qikan_id/d',1),
            'status'=>0,
        ]);

		$this->success('提交成功，等待付款',['order_id'=>$rs]);
	}

	public function submit_fukuan(Request $request){
	    $rs=Db::name('qikandingyue')->where([
	            'user_id'=>$this->auth->id,
			    'id'=>$request->param('order_id/d',0),
	        ])->update([
			'status'=>1,
			'image'=>$request->param('image/s',''),
		]);
		if($rs>0){
		    $this->success('提交成功，等待审核');
		}else{
		    $this->error('提交失败');
		}
	}

	public function submit_tougao(Request $request){


	    $rs=Db::name('zaixiantougao')->insert([
	        'title'=>$request->param('title/s',''),
    	    'author'=>$request->param('author/s',''),
        	'tel'=>$request->param('tel/s',''),
        	'contact'=>$request->param('contact/s',''),
        	'createtime'=>time(),
        	'user_id'=>$this->auth->id,
        	'files'=>$request->param('files/s',''),
	    ]);
	    //echo Db::getLastSql();
		if($rs>0){
		    $this->success('提交成功，等待审核');
		}else{
		    $this->error('提交失败');
		}
	}

	public function submit_becompany(Request $request){
	    if($request->has('id')){
	        $rs=Db::name('qiyeshenpi')->where(['id'=>$request->param('id')])->update([

        	    'name'=>$request->param('name/s',''),
            	'image'=>$request->param('logo/s',''),
            	'netaddress'=>$request->param('netaddress/s',''),
            	'email'=>$request->param('email/s',''),
            	'tel'=>$request->param('tel/s',''),
            	'address'=>$request->param('address/s',''),
            	'content'=>$request->param('content/s',''),
            	'user_id'=>$this->auth->id,
            	'status'=>0,
            	'createtime'=>time(),
    	    ]);
	    }else{
	        $rs=Db::name('qiyeshenpi')->insertGetId([
    	        'randomid'=>create_guid(),
        	    'name'=>$request->param('name/s',''),
            	'image'=>$request->param('logo/s',''),
            	'netaddress'=>$request->param('netaddress/s',''),
            	'email'=>$request->param('email/s',''),
            	'tel'=>$request->param('tel/s',''),
            	'address'=>$request->param('address/s',''),
            	'content'=>$request->param('content/s',''),
            	'user_id'=>$this->auth->id,
            	'createtime'=>time(),
    	    ]);
	    }



		if($rs>0){
		    $this->success('提交成功，等待审核',['id'=>$rs]);
		}else{
		    $this->error('提交失败');
		}
	}

	public function product_xiajia(Request $request){
	    $id=$request->param('id/d',0);
	    $qiye=Db::name('qiye')->where('user_id',$this->auth->id)->find();
	    $rs=Db::name('product')->where([
	        'id'=>$id,
	        'qiye_id'=>$qiye['id'],
	    ])->update(['status'=>'1']);

	    if($rs>0){
		    $this->success('操作成功');
		}else{
		    $this->error('操作失败');
		}
	}

	public function product_shangjia(Request $request){
	    $id=$request->param('id/d',0);
	    $qiye=Db::name('qiye')->where('user_id',$this->auth->id)->find();
	    $rs=Db::name('product')->where([
	        'id'=>$id,
	        'qiye_id'=>$qiye['id'],
	    ])->update(['status'=>'2']);

	    if($rs>0){
		    $this->success('操作成功');
		}else{
		    $this->error('操作失败');
		}
	}

	public function product_shanchu(Request $request){
	    $id=$request->param('id/d',0);
	    $qiye=Db::name('qiye')->where('user_id',$this->auth->id)->find();
	    $rs=Db::name('product')->where([
	        'id'=>$id,
	        'qiye_id'=>$qiye['id'],
	    ])->delete();

	    if($rs>0){
		    $this->success('操作成功');
		}else{
		    $this->error('操作失败');
		}
	}

	public function add_product(Request $request){
	    $user_id=$this->auth->id;
	    $qiye=Db::name('qiye')->where('user_id',$this->auth->id)->find();

	    $rs=Db::name('product')->insertGetId([
	        'randomid'=>create_guid(),
    	    'name'=>$request->param('name/s',''),
        	'image'=>$request->param('image/s',''),
        	'chanpinxinghao'=>$request->param('chanpinxinghao/s',''),
        	'category_id'=>$request->param('category_id/d',''),
        	'yongtu_content'=>$request->param('yongtu_content/s',''),
        	'tedian_content'=>$request->param('tedian_content/s',''),
        	'qiye_id'=>$qiye['id'],
        	'updatetime'=>time(),
	    ]);

	    if($rs>0){
		    $this->success('操作成功');
		}else{
		    $this->error('操作失败');
		}
	}

	public function edit_product(Request $request){
	    $user_id=$this->auth->id;
	    $qiye=Db::name('qiye')->where('user_id',$this->auth->id)->find();

	    $rs=Db::name('product')->where([
	        'qiye_id'=>$qiye['id'],
	        'id'=>$request->param('id/d',0),
	    ])->update([
    	    'name'=>$request->param('name/s',''),
        	'image'=>$request->param('image/s',''),
        	'chanpinxinghao'=>$request->param('chanpinxinghao/s',''),
        	'category_id'=>$request->param('category_id/d',''),
        	'yongtu_content'=>$request->param('yongtu_content/s',''),
        	'tedian_content'=>$request->param('tedian_content/s',''),
        	'updatetime'=>time(),
	    ]);

	    if($rs>0){
		    $this->success('操作成功');
		}else{
		    $this->error('操作失败');
		}
	}

	public function get_children_category(Request $request){
		$id=$request->param('pid/d',0);
		$category=Db::name('category')->where('pid',$id)->select();
		foreach($category as $k=>$v){
			$category[$k]['list']=Db::name('category')->where('pid',$v['id'])->select();
		}
		$this->success('操作成功',$category);
	}
}
